package com.taobao.top
{
	import com.adobe.serialization.json.JSON;
	import com.taobao.top.converter.TradeRateConverter;
	import com.taobao.top.resultmodel.TradeRateAddResult;
	import com.taobao.top.resultmodel.TradeRatesGetResult;
	
	import flash.events.Event;
	/**
	 * 评价 
	 * @author 达达
	 * 
	 */	
	public class TOPTradeRate
	{
		public function TOPTradeRate()
		{
		}
		
		/**
		 * 搜索评价信息 
		 * @param fields
		 * @param rate_type
		 * @param role
		 * @param result
		 * @param page_no
		 * @param page_size
		 * 
		 */		
		public function getTradeRates(fields:Array,rate_type:String,role:String,result:String="",page_no:int = 1,page_size:int = 40):void
		{
			var topURLLdr:TOPURLLoader = new TOPURLLoader();
			var reqParamArr:Array = new Array();
			reqParamArr=[new TopRequestParam("method","taobao.traderates.get"),new TopRequestParam("fields",TOPUtil.StringArrayToStringWidthComma(fields)),new TopRequestParam("rate_type",rate_type),new TopRequestParam("role",role)];
			if(result!= "")
			{
				reqParamArr.push(new TopRequestParam("result",result));
			}
			
			if(page_no!= 1)
			{
				reqParamArr.push(new TopRequestParam("page_no",page_no.toString()));
			}
			
			if(page_size!= 40)
			{
				reqParamArr.push(new TopRequestParam("page_size",page_size));
			}
			topURLLdr.addEventListener(Event.COMPLETE,usersLoadHandler);
			topURLLdr.loadData(reqParamArr);
		}
		
		/**
		 * 新增单个评价 
		 * @param tid
		 * @param content
		 * @param result
		 * @param anony
		 * @param role
		 * @param order_id
		 * 
		 */		
		public function addTradeRate(tid:String,content:String,result:String,anony:String,role:String,order_id:String= ""):void
		{
			var topURLLdr:TOPURLLoader = new TOPURLLoader();
			var reqParamArr:Array = new Array();
			reqParamArr=[new TopRequestParam("method","taobao.traderate.add"),new TopRequestParam("tid",tid),new TopRequestParam("content",content),new TopRequestParam("result",result),new TopRequestParam("anony",anony),new TopRequestParam("role",role)];
			if(order_id!= "")
			{
				reqParamArr.push(new TopRequestParam("order_id",order_id));
			}
			
			topURLLdr.addEventListener(Event.COMPLETE,tradeRateAddResult);
			topURLLdr.loadData(reqParamArr);
		}
		
				
		public function addTradeRateList(tid:String,content:String,result:String,anony:String,role:String):void
		{
			var topURLLdr:TOPURLLoader = new TOPURLLoader();
			var reqParamArr:Array = new Array();
			reqParamArr=[new TopRequestParam("method","taobao.traderate.list.add"),new TopRequestParam("tid",tid),new TopRequestParam("content",content),new TopRequestParam("result",result),new TopRequestParam("anony",anony),new TopRequestParam("role",role)];
			topURLLdr.addEventListener(Event.COMPLETE,tradeRateListAddResult);
			topURLLdr.loadData(reqParamArr);
		}
		
		private function tradeRatesGetResult(event:Event):void
		{
			switch(event.type)
			{
				case Event.COMPLETE:
					var tradeRatesGetResult:TradeRatesGetResult = tradeRatesGetJsonParser(event.target.data);
				break;
				default:
					 dispatchEvent(event.clone());
				break;
			}
		}
		
		private function tradeRatesGetJsonParser(jsonStr:String):TradeRatesGetResult
		{
			if(TOPError.isError(jsonStr))
			{
				var topError:TOPError = TOPError.parseError(jsonStr);
				//dispatchEvent(new ErrorEvent(ErrorEvent.ERROR,false,false,
			}else
			{
				var tradeRatesGetObj:Object = JSON.decode(jsonStr).rsp;
				var tradrRatesGetResult:TradeRatesGetResult = new TradeRatesGetResult();
				tradrRatesGetResult.totalResults = tradeRatesGetObj.totalResults;
				for (var i:int = 0 ;i<tradrRatesGetResult.totalResults;i++)
				{
					tradrRatesGetResult.rates.push(TradeRateConverter.convert(tradeRatesGetObj.rates[i]));
				}
				return tradrRatesGetResult;
			}
			return null;
		}
		
		private function tradeRatesGetResult(event:Event):void
		{
			switch(event.type)
			{
				case Event.COMPLETE:
					var tradeRateAddResult:TradeRateAddResult = tradeRateAddJsonParser(event.target.data);
				break;
				default:
					 dispatchEvent(event.clone());
				break;
			}
		}
		
		private function tradeRateAddJsonParser(jsonStr:String):TradeRateAddResult
		{
			if(TOPError.isError(jsonStr))
			{
				var topError:TOPError = TOPError.parseError(jsonStr);
				//dispatchEvent(new ErrorEvent(ErrorEvent.ERROR,false,false,
			}else
			{
				var tradeRateAddObj:Object = JSON.decode(jsonStr).rsp;
				var tradrRateAddResult:TradeRateAddResult = new TradeRateAddResult();
				tradrRateAddResult.created = TOPUtil.getCustomDate(tradeRatesGetObj.created.toString());
				tradrRateAddResult.order_id = tradeRateAddObj.order_id;
				tradrRateAddResult.tid = tradeRateAddObj.tid;
				return tradrRateAddResult;
			}
			return null;
		}
		
		private function tradeRateListAddResult(event:Event):void
		{
			switch(event.type)
			{
				case Event.COMPLETE:
					var tradeRateListAddResult:TradeRateListAddResult = tradeRateListAddJsonParser(event.target.data);
				break;
				default:
					 dispatchEvent(event.clone());
				break;
			}
		}
		
		private function tradeRateListAddJsonParser(jsonStr:String):TradeRateListAddResult
		{
			if(TOPError.isError(jsonStr))
			{
				var topError:TOPError = TOPError.parseError(jsonStr);
				//dispatchEvent(new ErrorEvent(ErrorEvent.ERROR,false,false,
			}else
			{
				var tradeRateListAddObj:Object = JSON.decode(jsonStr).rsp;
				var tradrRateAddListResult:TradeRateListAddResult = new TradeRateListAddResult();
				tradrRateAddListResult.created = TOPUtil.getCustomDate(tradeRatesGetObj.created.toString());
				tradrRateAddListResult.tid = tradeRatesGetObj.tid;
				return TradeRateListAddResult;
			}
			return null;
		}
	}
}